<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class LoginController extends Controller
{

	public function index()
	{
		$list=DB::table('type')->get();
        $config=DB::table('config')->get();
		return view('home.login.login',['list'=>$list,'config'=>$config]);
	}

	public function dologin(Request $request)
    {
        $name = $request->input('username');
        $ob = DB::table("user")->where('username',$name)->first();
    	//如果通过这个用户名能查出数据
    	if($ob && $ob->state==0){
    		if(md5($request->input('pass')) == $ob->pass){
	    		//如果都能通过，则把用户信息存储到session中
				session(['homeuser'=>$ob]);
				//重定向到前台页面
				return redirect('/home');
			}else{
				return back()->with('msg','登录失败：用户名或密码错误');
			}
    	}else{
			return back()->with('msg','登录失败：用户受限，请联系后台管理员');
		}
    }

    public function logout()
    {
        // 销毁session
        session()->forget('homeuser');
        return redirect('/home');
    }


}
